package com.suda.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suda.dao.OrderRouteMapper;
import com.suda.pojo.OrderRoute;
import com.suda.service.OrderRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:zhuyongjie
 * @Description:OrderRoute业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class OrderRouteServiceImpl implements OrderRouteService {

    @Autowired
    private OrderRouteMapper orderRouteMapper;


    /**
     * OrderRoute条件+分页查询
     * @param orderRoute 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<OrderRoute> findPage(OrderRoute orderRoute, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(orderRoute);
        //执行搜索
        return new PageInfo<OrderRoute>(orderRouteMapper.selectByExample(example));
    }

    /**
     * OrderRoute分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<OrderRoute> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<OrderRoute>(orderRouteMapper.selectAll());
    }

    /**
     * OrderRoute条件查询
     * @param orderRoute
     * @return
     */
    @Override
    public List<OrderRoute> findList(OrderRoute orderRoute){
        //构建查询条件
        Example example = createExample(orderRoute);
        //根据构建的条件查询数据
        return orderRouteMapper.selectByExample(example);
    }


    /**
     * OrderRoute构建查询对象
     * @param orderRoute
     * @return
     */
    public Example createExample(OrderRoute orderRoute){
        Example example=new Example(OrderRoute.class);
        Example.Criteria criteria = example.createCriteria();
        if(orderRoute!=null){
            // 路由id
            if(!StringUtils.isEmpty(orderRoute.getRouteId())){
                    criteria.andEqualTo("routeId",orderRoute.getRouteId());
            }
            // 订单id
            if(!StringUtils.isEmpty(orderRoute.getOrderId())){
                    criteria.andEqualTo("orderId",orderRoute.getOrderId());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        orderRouteMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改OrderRoute
     * @param orderRoute
     */
    @Override
    public void update(OrderRoute orderRoute){
        orderRouteMapper.updateByPrimaryKey(orderRoute);
    }

    /**
     * 增加OrderRoute
     * @param orderRoute
     */
    @Override
    public void add(OrderRoute orderRoute){
        orderRouteMapper.insert(orderRoute);
    }

    /**
     * 根据ID查询OrderRoute
     * @param id
     * @return
     */
    @Override
    public OrderRoute findById(String id){
        return  orderRouteMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询OrderRoute全部数据
     * @return
     */
    @Override
    public List<OrderRoute> findAll() {
        return orderRouteMapper.selectAll();
    }
}
